package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.dto.GoodsDto;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.*;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/23 8:57
 */
@RestController
@RequestMapping("/admin/goods")
@Slf4j
@CrossOrigin
public class GoodsController {
    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsCategoryService dtsCategoryService;
    @Autowired
    private DtsGoodsAttributeService dtsGoodsAttributeService;
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    private DtsGoodsSpecificationService dtsGoodsSpecificationService;
    @Autowired
    private DtsBrandService dtsBrandService;

    //  //ttp://localhost:8080/admin/brand/list?page=1&limit=20&id=xx&name=xd&sort=add_time&order=de
    //    @GetMapping("")
    //接口地址 :'http://localhost:8080/admin/goods/list?page=1&limit=20&sort=add_time&order=desc
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="查找")
    @RequiresPermissions("admin:goods:list")
    @GetMapping("/list")
    public Object goodsLIst(Integer page, Integer limit, String sort, String order, String name, Integer goodsSn) {
        //page=1&limit=20&goodsSn=ss&name=ss&sort=add_time&order=desc
        Page<DtsGoods> dtsGoodsPage = dtsGoodsService.selectPage(page, limit, sort, order, name, goodsSn);


        List<DtsGoods> records = dtsGoodsPage.getRecords();
        long total = dtsGoodsPage.getTotal();

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);


    }

    //接口地址 : http://localhost:8083/admin/goods/detail?id=23858
    @GetMapping("/detail")
    public Object OrderDetail(Integer id) {
        DtsGoods dtsGoods = dtsGoodsService.selectById(id);
        Set<Integer> categoryIds = dtsCategoryService.selectById(dtsGoods.getCategoryId());
        List<DtsGoodsAttribute> dtsGoodsAttribute = dtsGoodsAttributeService.selectByGoodsId(id);
        List<DtsGoodsProduct> dtsGoodsProduct = dtsGoodsProductService.selectByGoodsId(id);
        List<DtsGoodsSpecification> dtsGoodsSpecification = dtsGoodsSpecificationService.selectByGoodsId(id);
        HashMap hashMap = new HashMap();
        hashMap.put("categoryIds", categoryIds);
        hashMap.put("goods", dtsGoods);
        hashMap.put("attributes", dtsGoodsAttribute);
        hashMap.put("specifications", dtsGoodsSpecification);
        hashMap.put("products", dtsGoodsProduct);
        return ResponseUtil.ok(hashMap);

    }

    //http://localhost:8080/admin/goods/catAndBrand
    @GetMapping("/catAndBrand")
    public Object catAndBrand() {
//categoryList
        Set<CatVo> catVos = dtsCategoryService.selectCategoryAllList();
        Set<CatVo> brandList = dtsBrandService.select();
        HashMap hashMap = new HashMap();
        hashMap.put("categoryList", catVos);
        hashMap.put("brandList", brandList);
        return ResponseUtil.ok(hashMap);
    }

    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="编辑")
    @RequiresPermissions("admin:goods:update")
    @PostMapping("/update")
    public Object categoryupdate(@RequestBody GoodsDto goodsDto) {
        List<DtsGoodsAttribute> attributes = goodsDto.getAttributes();
        DtsGoods goods = goodsDto.getGoods();
        List<DtsGoodsProduct> products = goodsDto.getProducts();
        List<DtsGoodsSpecification> specifications = goodsDto.getSpecifications();

        if(goods==null){
            return ResponseUtil.fail();
        }
        if(attributes==null){
            return ResponseUtil.fail();
        }
        if(products==null){
            return ResponseUtil.fail();
        }
        if(specifications==null){
            return ResponseUtil.fail();
        }
        goods.setUpdateTime(new Date());
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
        }
        for (DtsGoodsProduct product : products) {
            product.setUpdateTime(new Date());
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            ;
        }

        dtsGoodsProductService.update(products, goods.getId());
        dtsGoodsService.update(goods);
        dtsGoodsAttributeService.update(attributes, goods.getId());
        dtsGoodsSpecificationService.update(specifications, goods.getId());
        return ResponseUtil.ok();

    }
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="删除")
    @RequiresPermissions("admin:goods:delete")
    @PostMapping("/delete")
    public Object goodsDelete(@RequestBody DtsGoods dtsGoods) {
        if(dtsGoods==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsGoods.setUpdateTime(new Date());
        dtsGoods.setDeleted(true);
        dtsGoodsService.delete(dtsGoods);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="添加")
    @RequiresPermissions("admin:goods:create")
    @PostMapping("/create")
    public Object goodsCreate(@RequestBody GoodsDto goodsDto) {

        List<DtsGoodsAttribute> attributes = goodsDto.getAttributes();
        DtsGoods goods = goodsDto.getGoods();
        dtsGoodsService.insert(goods);
        // DtsGoods goods1 =   dtsGoodsService.selectByGoodsSn(goods.getGoodsSn());
        List<DtsGoodsProduct> products = goodsDto.getProducts();
        List<DtsGoodsSpecification> specifications = goodsDto.getSpecifications();
        goods.setUpdateTime(new Date());
        goods.setAddTime(new Date());
if(goods==null){
    return ResponseUtil.fail();
}
        if(attributes==null){
            return ResponseUtil.fail();
        }
        if(products==null){
            return ResponseUtil.fail();
        }
        if(specifications==null){
            return ResponseUtil.fail();
        }

        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            attribute.setAddTime(new Date());
            attribute.setGoodsId(goods.getId());
        }
        for (DtsGoodsProduct product : products) {
            product.setUpdateTime(new Date());
            product.setAddTime(new Date());
            ;
            product.setGoodsId(goods.getId());
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            specification.setAddTime(new Date());
            specification.setGoodsId(goods.getId());
        }
        dtsGoodsProductService.insert(products);

        dtsGoodsAttributeService.insert(attributes);
        dtsGoodsSpecificationService.insert(specifications);
        return ResponseUtil.ok();
    }
}
